package com.xianbeiying.app.ckgl;

import android.app.Application;
import android.os.Environment;
import android.support.v4.content.LocalBroadcastManager;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.xianbeiying.app.ckgl.bean.AddressBean;
import com.xianbeiying.app.ckgl.bean.CropsBean;
import com.xianbeiying.app.ckgl.bean.DevicesBean;
import com.xianbeiying.app.ckgl.bean.DpWatherBean;
import com.xianbeiying.app.ckgl.bean.JINGXUNBean;
import com.xianbeiying.app.ckgl.bean.LoginBean;
import com.xianbeiying.app.ckgl.bean.NoticesBean;
import com.xianbeiying.app.ckgl.bean.WeatherBean;
import com.xianbeiying.app.ckgl.util.CrashHandler;
import com.xianbeiying.app.ckgl.util.RegUtil;
import com.xianbeiying.app.ckgl.util.SharedPreferencesUtils;
import com.xianbeiying.app.ckgl.widght.AppManager;

import java.io.File;
import java.util.List;

/**
 * 应用类
 */
public class MyApplication  extends Application implements AMapLocationListener {
    //登陆返回信息
    public static LoginBean loginBean;
    //全部的大棚
    public static DevicesBean devicesBean;
    //农作物
    public static  CropsBean cropsBean;
    //省市区
    public static AddressBean addressBean;
    private static MyApplication instance;
//    public static Cookie appCookie;
    // 默认存放图片的路径
    public final static String DEFAULT_SAVE_IMAGE_PATH = Environment.getExternalStorageDirectory() + File.separator + "zngg" + File.separator + "Images"
            + File.separator;
    public static boolean isLogin;
  //定位的
   public static AMapLocationClient locationClient = null;
    public static AMapLocationClientOption locationOption = null;
    public static LocalBroadcastManager broadcastManager;
   //天气
    public static WeatherBean weatherBean;
    public static DpWatherBean dpWatherBean;
//    public static String IP="121.40.56.134";
    public static String IP="120.26.96.123";
//        public static String IP="192.168.1.117";
    //时间日期数据
    public static List<String> year, month;
    public static int yearId, monthId;
    public static NoticesBean noticesBean;
    public static JINGXUNBean jingxunBean;

    // 用于存储变量的；
    public boolean isDownload;
    public static  boolean isShowDown=true;//先显示提示框
    public static boolean isDownFile=true;//是否进行调用下载接口
    public String versionsUrl, versionsName;
    public static MyApplication getInstance() {
        return instance;
    }
    @Override
    public void onCreate(){
        super.onCreate();
        instance=this;
        AppManager.getInstance().init(this);
        //图片加载的处理
        initImageLoader();
        //初始化
        SharedPreferencesUtils.initSharedPreferences(getApplicationContext());
        // 设置定位参数
        gaodeInvite();
        locationClient.setLocationOption(locationOption);
        // 启动定位
        locationClient.startLocation();
        //时间日期数据
        year = com.xianbeiying.app.ckgl.util.DataUtil.getTimeHHs();
        yearId = com.xianbeiying.app.ckgl.util.DataUtil.timeHHid;
        month = com.xianbeiying.app.ckgl.util.DataUtil.getTimeMMs();
        monthId = com.xianbeiying.app.ckgl.util.DataUtil.timeMMid;
        CrashHandler.getInstance().init(this);
    }
    private void initImageLoader() {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
//                .showImageForEmptyUri(R.mipmap.card_img) //设置图片Uri为空或是错误的时候显示的图片
//                .showImageOnFail(R.mipmap.card_img)      //设置图片加载/解码过程中错误时候显示的图片
//                .showImageOnLoading(R.mipmap.card_img) //设置图片在下载期间显示的图片
                .cacheInMemory(true)                 //设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)             // //设置下载的图片是否缓存在SD卡中
                .build();
        File cacheDir = new File(DEFAULT_SAVE_IMAGE_PATH);
        ImageLoaderConfiguration imageconfig = new ImageLoaderConfiguration.Builder(this)
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .diskCacheSize(50 * 1024 * 1024)
                .diskCacheFileCount(200)
                .diskCache(new UnlimitedDiskCache(cacheDir))
                .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                .defaultDisplayImageOptions(options).build();
        ImageLoader.getInstance().init(imageconfig);
    }
    //定位返回的数据对象
    public static AMapLocation myMapLocation;
    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
//        LogUtils.e("定位",aMapLocation.getErrorCode()+"---------"+aMapLocation.getErrorInfo());
        if (aMapLocation != null){
            //定位成功
            if (aMapLocation.getErrorCode() == 0){
                this.myMapLocation = aMapLocation;
                //当定位成功时,关闭定位
//                LogUtils.e("定位",aMapLocation.getCity());
                locationClient.stopLocation();
                //参数返回值
                if (RegUtil.back() != null){
                    RegUtil.back().onCityBackData(aMapLocation);
                }
            }
        }else{
            if (RegUtil.back() != null){
                RegUtil.back().onCityBackData(null);
            }
        }

    }

    private void gaodeInvite() {
        locationClient = new AMapLocationClient(this.getApplicationContext());
        locationOption = new AMapLocationClientOption();
        // 设置定位模式为高精度模式
        locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        // 设置定位监听
        locationClient.setLocationListener(this);
//        设置参数
        // 设置是否需要显示地址信息
        locationOption.setNeedAddress(true);
        /**
         * 设置是否优先返回GPS定位结果，如果30秒内GPS没有返回定位结果则进行网络定位
         * 注意：只有在高精度模式下的单次定位有效，其他方式无效
         */
        locationOption.setGpsFirst(true);
        locationOption.setOnceLocation(false);
        // 设置发送定位请求的时间间隔,最小值为1000，如果小于1000，按照1000算
        locationOption.setInterval(Long.valueOf(1000));
    }

    public void setVersionsName(String versionsName) {
        this.versionsName = versionsName;
    }

    public String getVersionsUrl() {
        return versionsUrl;
    }

    public void setVersionsUrl(String versionsUrl) {
        this.versionsUrl = versionsUrl;
    }

    public boolean isDownload() {
        return isDownload;
    }

    public void setDownload(boolean isDownload){
        this.isDownload = isDownload;
    }

}
